from wtforms import Form,StringField,IntegerField,validators,ValidationError
from wtforms.validators import Length,InputRequired,Email,EqualTo
from ..forms import BaseForm
from utils import dxcache
from flask import g
class LoginForm(BaseForm):
    email = StringField(validators=([Email(message='请输入正确的邮箱格式'),InputRequired(message='请输入邮箱')]))
    password = StringField(validators=([Length(6,20,message='请输入正确格式的密码'),InputRequired(message='请输入密码')]))
    remember = IntegerField()

class ResetpwdForm(BaseForm):
    oldpwd = StringField(validators=([Length(6,20,message='请输入正确格式的旧密码')]))
    newpwd = StringField(validators=([Length(6,20,message='请输入正确格式的新密码')]))
    newpwd2 = StringField(validators=([EqualTo('newpwd')]))

class ResetemailForm(BaseForm):
    email = StringField(validators=[Email(message='请输入正确的邮箱！')])
    captcha = StringField(validators=[Length(min=4, max=4, message='请输入正确长度的验证码！')])
    def validate_captcha(self,field):
        captcha = field.data
        email = self.email.data
        captcha_cache = dxcache.get(email)
        if not captcha_cache or captcha.lower() != captcha_cache.lower():
            raise ValidationError('邮箱验证码错误！')

    def validate_email(self,field):
        email = field.data
        user = g.cms_user
        if user.email == email:
            raise ValidationError('邮箱不能重复！')


class AddBannerForm(BaseForm):
    name = StringField(validators=[InputRequired(message='轮播图名称不能为空！')])
    image_url = StringField(validators=[InputRequired(message='图片地址不能为空！')])
    link_url = StringField(validators=[InputRequired(message='跳转地址不能为空！')])
    priority = IntegerField(validators=[InputRequired(message='输入优先级！')])


class UpdateBannerForm(AddBannerForm):
    banner_id = StringField(validators=[InputRequired(message='请传入正确的banner_id!')])

class AddBoardForm(BaseForm):
    name = StringField(validators=[InputRequired(message='请输入版块名称！')])

class UpdateBoardForm(AddBoardForm):
    board_id = StringField(validators=[InputRequired(message='请输入板块ID')])


